//
//  HeartViewController.m
//  你的温实初
//
//  Created by lanou on 16/7/15.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "HeartViewController.h"

@interface HeartViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation HeartViewController

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
    }
    return _tableview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableview];
    self.navigationItem.title = @"心理";
    [self.tableview registerNib:[UINib nibWithNibName:@"HeartTableViewCell" bundle:nil] forCellReuseIdentifier:@"HeartModel_cell"];
    
    // 请求数据
    [RequestManager fetchDataWithUrl:kHeartUrl Parameter:@{} Method:@"POST" Compelet:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        for (NSDictionary *dic in bigDic[@"datasource"]) {
            HeartModel *model = [HeartModel new];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSource addObject:model];
        }
        
    } updateUI:^{
        [self.tableview reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ----------tableview---------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HeartModel *model = self.dataSource[indexPath.row];
    BaseCell *cell = [FctoryModel productCellWithModel:model tableView:tableView indexPath:indexPath];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.view.frame.size.height/6;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HeartModel *model = self.dataSource[indexPath.row];
    HeartListViewController *heartListVC = [HeartListViewController new];
    heartListVC.ID = model.ID;
    [self.navigationController pushViewController:heartListVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
